package com.wxx.utils;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.internal.DefaultCommentGenerator;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

/**
 * 每次改动都要重新install到本地仓库，否则不生效
 * 每次改动都要重新install到本地仓库，否则不生效
 * 每次改动都要重新install到本地仓库，否则不生效
 * 每次改动都要重新install到本地仓库，否则不生效
 * 每次改动都要重新install到本地仓库，否则不生效
 * @author wangxiaoxian
 * @since 2020/9/10
 */
public class DBCommentGenerator extends DefaultCommentGenerator {
    private Properties properties;

    public DBCommentGenerator() {
        super();
        properties = new Properties();
    }

    @Override
    public void addConfigurationProperties(Properties properties) {
        // 获取自定义的 properties
        this.properties.putAll(properties);
    }

    @Override
    public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        topLevelClass.addImportedType("io.swagger.annotations.ApiModelProperty");
        // 获取表注释
        topLevelClass.addJavaDocLine("/**");
        topLevelClass.addJavaDocLine(" * " +(introspectedTable.getRemarks() == null ? "" : introspectedTable.getRemarks()));
        topLevelClass.addJavaDocLine(" * @author " + System.getProperty("user.name"));
        topLevelClass.addJavaDocLine(" * @since " + new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        topLevelClass.addJavaDocLine(" */");
    }

    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        // 获取列注释
//        field.addJavaDocLine("/** " + (introspectedColumn.getRemarks() == null ? "" : introspectedColumn.getRemarks()) + " */");
        field.addJavaDocLine("@ApiModelProperty(\"" + (introspectedColumn.getRemarks() == null ? "" : introspectedColumn.getRemarks()) + "\")");
    }
}
